﻿using System;
using System.Data.Entity;
using System.Windows.Forms;

namespace Database.Forms
{
    /// <summary>
    /// The main db form.
    /// </summary>
    public partial class MainDbForm : Form
    {
        private DbSimEntities _entities;

        public MainDbForm()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            try
            {
                base.OnLoad(e);
                _entities = new DbSimEntities();
                _entities.Parameter.Load();
                _entities.Category.Load();
                _entities.Actions.Load();
                _entities.Resources.Load();
                _entities.Summary.Load();

                categoryBindingSource.DataSource = _entities.Category.Local.ToBindingList();
                actionsBindingSource.DataSource = _entities.Actions.Local.ToBindingList();
                parameterBindingSource.DataSource = _entities.Parameter.Local.ToBindingList();
                resourcesBindingSource.DataSource = _entities.Resources.Local.ToBindingList();
                summaryBindingSource.DataSource = _entities.Summary.Local.ToBindingList();
            }
            catch
            {
                MessageBox.Show("Failed connect to database!");
            }
        }

        #region open edit forms
        private void button1_Click(object sender, EventArgs e)
        {
            Form action = new ActionForm();
            action.Show();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Form param = new ParameterForm();
            param.Show();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Form res = new ResourceForm();
            res.Show();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Form category = new CategoryForm();
            category.Show();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            Form summary = new SummaryForm();
            summary.Show();
        }
        
        #endregion

        #region refresh
        private void button4_Click(object sender, EventArgs e)
        {
            OnLoad(e);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OnLoad(e);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            OnLoad(e);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            OnLoad(e);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            OnLoad(e);
        }

        #endregion
    }
}
